package com.odm.communitymanage.ui.activity.states

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.MyActivityData
import com.odm.communitymanage.ui.activity.*
import kotlinx.android.synthetic.main.fragment_ongoing_activities.*

/**
 * @description: 进行中活动列表页面
 * @author: ODM
 * @date: 2020/5/13
 */
class OngoingActivitiesFragment : BaseMVVMFragment() {

    var rvAdapter : ActivitiesAdapter?= null
    var viewModel : ActivitiesViewModel?= null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewModel = ViewModelProviders.of(this).get(ActivitiesViewModel::class.java)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        msv_activities_ongoing?.showLoading()
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        viewModel?.getOngoingList()
    }

    override fun initViews() {
    }

    private fun  initOngoingActivityRecyclerView(){
        rvOngoingActivities?.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = ActivitiesAdapter(mutableListOf())
        rvOngoingActivities?.adapter  = rvAdapter
        rvAdapter?.animationEnable = true
        rvAdapter?.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInBottom)
        //设置空布局
        rvAdapter?.setEmptyView(R.layout.layout_empty_list)
        //item 的点击事件
        rvAdapter?.setOnItemClickListener{ adapter, view, position ->
            //传递数据和事件
            LiveEventBus.get(Constants.EVENT_ACTIVITY_DETAIL)
                .post(rvAdapter?.data?.get(position))
            //从容器Activity中找出父Fragment，调用启动 通知详情页
            _mActivity.findFragment( ActivityListFragment::class.java).startDetailFragment()
        }
    }

    override fun initViewModelObserve() {
        //观察请求返回的列表
        viewModel?.activityList?.observe(this ,object : Observer<List<MyActivityData>> {
            override fun onChanged(data : List<MyActivityData>) {
                //加载列表
                if(rvOngoingActivities.adapter == null){
                    initOngoingActivityRecyclerView()
                }
                // 列表数据为空，显示空状态布局
                // 列表数据不为空根据Adapter数据引用判断使用setList/setDiffNewData
                if(data.isEmpty()){
                    msv_activities_ongoing?.showEmpty()
                }else {
                    msv_activities_ongoing?.showContent()
                    if( rvAdapter?.data?.isEmpty() == true ){
                        rvAdapter?.setList(data.toMutableList())
                    }else {
                        rvAdapter?.setDiffNewData(DiffUtil.calculateDiff(ActivitiesDiffCallback(rvAdapter?.data?.toList(), data)) ,data.toMutableList())
                    }
                }

            }
        })
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_ongoing_activities

    }

    override fun onBackPressedSupport(): Boolean {
        return false
    }


}